package com.xiaoyu.lanling.feature.serverpush.event

import `in`.srain.cube.concurrent.SimpleTask
import `in`.srain.cube.request.JsonData
import `in`.srain.cube.util.CLog
import androidx.fragment.app.FragmentActivity
import com.xiaoyu.base.app.App
import com.xiaoyu.base.serverpush.event.ServerPushBaseEvent
import com.xiaoyu.lanling.feature.moment.activity.PublishActivity
import com.xiaoyu.lanling.feature.prize.PrizeDialogFragment

class PrizePush(val key: String, val jsonData: JsonData) : ServerPushBaseEvent(jsonData) {

    override fun takeAction() {
        setComplete()
        val activity = (App.getInstance().topActivity as? FragmentActivity) ?: return
        if (activity.isFinishing || activity.isDestroyed || activity is PublishActivity) {
            SimpleTask.postDelay({
                showPrizeDialogIfPossible()
            }, 160)
            return
        }
        showPrizeDialogIfPossible()
    }

    private fun showPrizeDialogIfPossible() {
        val activity = (App.getInstance().topActivity as? FragmentActivity) ?: return
        if (activity.isFinishing || activity.isDestroyed) {
            return
        }
        PrizeDialogFragment.showIfNeed(activity.supportFragmentManager, key, jsonData)
    }
}